from typing import Union, Dict

from fastapi import FastAPI, Path
from fastapi.params import Query
from pydantic import BaseModel
app = FastAPI()


class Item(BaseModel):
    name: str
    description: str | None = None
    price: float
    tax: float | None = None


@app.get("/")
async def root():
    return {"message": "Hello World"}


@app.get("/hello/{name}")
async def say_hello(name: str):
    return {"message": f"Hello {name}"}


# @app.get("/items/{item_id}")
# async def read_item(item_id: int, q: Union[str, None] = None):
#     return {"item_id": item_id, "q": q}


from typing import Annotated


# @app.get("/items/{item_id}")
# async def read_items(q: str, item_id: int = Path(description='物品ID，必须大于2', gt=2)):
#     results = {"item_id": item_id}
#     if q:
#         results.update({"q": q})
#     return results


@app.get("/items/")
async def read_items(q: str|None = Query(default=None,min_length=3)):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results


@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item, q: str | None = None):
    print(f"item:{item}")
    result = {"item_id": item_id, **item.dict()}
    print(f"result:{result}")
    if q:
        result.update({"q": q})
    return result
from fastapi import Depends

async def common(q: str|None=None, skip: int=0, limit: int=10):
    return {"q": q, "skip": skip, "limit": limit}


@app.get('/items')
async def read_items(common: Dict=Depends(common)):
    print(common.get('q'), common.get('skip'), common.get('limit'))
    return {"message": "ok"}

@app.get("/users")
async def read_users(common: dict = Depends(common)):
    return common